﻿using System;
using System.Collections.Generic;
using System.Text;

namespace I_C
{
    class I2CMaster : I2CDevice
    {
        enum STATE
        {
            IDLE,
            STOP,
            WAIT_START_ACK,
            WAIT_DATA_ACK,
            SEND_DATA,
            RECEIVE_DATA
        }


        // ========== Current state
        STATE _currentState;

        // ============================================================================
        /// <summary> Updates the current logic for the master device </summary>
        // ============================================================================
        public override bool Update( OPERATION operation , byte data)
        {
            switch (_currentState)
            {
                case STATE.IDLE:
                    if (operation != OPERATION.START)
                    {
                        _currentState = STATE.IDLE;
                        return false;
                    }
                    _currentState = STATE.WAIT_START_ACK;
                    break;
                case STATE.WAIT_START_ACK:
                    if (operation == OPERATION.ACK)
                    {
                        if ((data & 0x01) == 0x01)
                        {
                            _currentState = STATE.RECEIVE_DATA;
                        }
                        else
                        {
                            _currentState = STATE.SEND_DATA;
                        }
                    }
                    else
                    {
                        _currentState = STATE.IDLE;
                        return false;
                    }
                    break;
                case STATE.SEND_DATA:
                    _currentState = STATE.WAIT_DATA_ACK;
                    break;
                case STATE.RECEIVE_DATA:
                    break;
                case STATE.WAIT_DATA_ACK:
                    break;
            }
            return false;
        }
    }
}
